package com.brillilab.service.job.kit;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.domain.enums.hardware.LockOperationEnum;
import com.brillilab.domain.enums.kit.KitUsageEnum;
import com.brillilab.domain.po.hardware.LockOperation;
import com.brillilab.service.core.hardware.ILockOperationService;
import com.brillilab.service.logic.kit.KitUsageLogic;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 开锁列表，24小时未开门，状态为变为取用TAKE
 * @time    2019-02-20
 * @author  WuMenghao
 */
@Component
public class KitUsageJob implements Job {

    @Resource
    private KitUsageLogic kitUsageLogic;
    @Resource
    private ILockOperationService lockOperationService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //获取参数
        JobDataMap map=context.getJobDetail().getJobDataMap();
        Integer type=(Integer)map.get("type");
        List kitUsageIds= (ArrayList) map.get("items");
        Long operateId=(Long) map.get("operateId");

        //校验
        Assert.notNull(type,"type can not be null");
        Assert.isTrue(type.equals(KitUsageEnum.State.TAKING.getValue()) ||
                type.equals(KitUsageEnum.State.RETURNING.getValue()),"type value not exist");

        //逻辑
        if(type.equals(KitUsageEnum.State.TAKING.getValue())){
            kitUsageLogic.changeToTake(kitUsageIds);
        }else if(type.equals(KitUsageEnum.State.RETURNING.getValue())){
            kitUsageLogic.changeToNotReturn(kitUsageIds);
        }

        if(operateId!=null){
            LockOperation operation = lockOperationService.selectById(operateId);
            if(operation!=null){
                operation.setOperate(LockOperationEnum.LOCK.getValue());
                lockOperationService.update(operation);
            }
        }

    }
}
